(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	var uid = userinfo.user_id;


	$.plusReady(function() {

		mui.ajax({
			url: apiurl + "User/myWallet/",
			data: {
				'user_id': uid,
				'_token': userinfo.token
			},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				// 请求成功
				if(res.status == -3) {
					mui.toast(res.msg);
					app.out();
					return false;
				} else {
					userinfo.a1_money = res.data.a1_money;
					userinfo.a2_money = res.data.a2_money;
					userinfo.a3_money = res.data.a3_money;
					app.setState(userinfo);

					doc.getElementById('BTC_price').innerHTML = res.eth_data.BTC.price_usd;
					doc.getElementById('ETH_price').innerHTML = res.eth_data.ETH.price_usd;
					doc.getElementById('EOS_price').innerHTML = res.eth_data.EOS.price_usd;
					doc.getElementById('LTC_price').innerHTML = res.eth_data.LTC.price_usd;
					doc.getElementById('XRP_price').innerHTML = res.eth_data.XRP.price_usd;
					doc.getElementById('DOGE_price').innerHTML = res.eth_data.DOGE.price_usd;
				}
			},
			error: function(xhr, type, errorThrown) {
				mui.toast('操作失败！');
			}
		});

		//复制
		document.getElementById("copy_addr").addEventListener('tap', function() {
			var addr = doc.getElementById('address').innerHTML;
			if(mui.os.android) {
				var Context = plus.android.importClass("android.content.Context");
				var main = plus.android.runtimeMainActivity();
				var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
				plus.android.invoke(clip, "setText", addr);
			}
			if(mui.os.ios) {
				var UIPasteboard = plus.ios.importClass("UIPasteboard");
				var generalPasteboard = UIPasteboard.generalPasteboard();
				// 设置文本内容:
				generalPasteboard.setValueforPasteboardType("testValue", addr);
			}
			mui.toast('钱包地址复制成功!');
		})

		doc.getElementById('kay_jf').addEventListener('tap', function() {
			var datas = doc.querySelectorAll('.asset_list .mui-table-view .mui-table-view-cell.kapay_list');
			mui.each(datas, function(index, item) {
				item.style.display = "none";
			});

			//			if(doc.getElementById('yue_list').style.display == "none") {

			doc.getElementById('yue_list').style.display = "block";

			mui.ajax({
				url: apiurl + "Trade/kapprice/",
				data: {
					'user_id': uid,
					'_token': userinfo.token
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {

					console.log(JSON.stringify(res));

					if(res.status == -3) {
						mui.toast(res.msg);
						app.out();
						return false;
					} else if(res.status != 1) {
						mui.toast(res.msg);
					} else {

						var myChart = echarts.init(document.getElementById('main'), "macarons");

						var option = {
							title: {
								text: 'KAP price chart',
								/*subtext: 'dataZoom支持'*/
							},
							tooltip: {
								trigger: 'item',
								formatter: function(params) {
									var date = new Date(params.value[0]);
									data = date.getFullYear() + '-' +
										(date.getMonth() + 1) + '-' +
										date.getDate() + ' ' +
										date.getHours() + ':' +
										date.getMinutes();
									return data + '<br/>' +
										params.value[1] /* + ', ' + params.value[2]*/ ;
								}
							},
							toolbox: {
								show: true,
								feature: {
									mark: {
										show: true
									},
									dataView: {
										show: false,
										readOnly: false
									},
									restore: {
										show: false
									},
									saveAsImage: {
										show: false
									}
								}
							},
							/*
							dataZoom: {
								show: true,
								start: 0
							},*/
							/*legend: {
								data: ['series1']
							},*/
							grid: {
								y2: 50
							},
							xAxis: [{
								type: 'time',
								splitNumber: 10
							}],
							yAxis: [{
								type: 'value',
								scale: true,
								boundaryGap: [0.001, 0.001]
							}],
							series: [{
								name: 'KAP',
								type: 'line',
								smooth: true,
								showAllSymbol: true,
								symbolSize: 3,
								itemStyle: {
									normal: {
										areaStyle: {
											type: 'default'
										}
									}
								},
								data: (function() {
									var d = [];
									for(var x = 0; x < res.data.length; x++) {
										var it = res.data[x];

										d.push([
											parseInt(it.add_time) * 1000,
											parseFloat(it.price)
										]);
									}

									return d;

								})()
							}]
						};
						// 使用刚指定的配置项和数据显示图表
						myChart.setOption(option);
					}
				},
				error: function(xhr, type, errorThrown) {
					mui.toast('操作失败！');
				}
			});
			//			}
		}, false);

		doc.getElementById('ioc').addEventListener('tap', function() {
			doc.getElementById('yue_list').style.display = "none";
			var datas = doc.querySelectorAll('.asset_list .mui-table-view .mui-table-view-cell.kapay_list');
			mui.each(datas, function(index, item) {
				item.style.display = "block";
			})
		}, false);

		doc.getElementById('transaction').addEventListener('tap', function() {
			mui.alert('交易敬请期待!');
			return false;
		}, false);

/*		doc.getElementById('balance_record').addEventListener('tap', function() {
			//打开余额记录页
			mui.openWindow({
				id: 'balance_record.html',
				url: '/pages/balance_record.html',
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		}, false);

		doc.getElementById('record_point').addEventListener('tap', function() {
			//打开积分记录页
			mui.openWindow({
				id: 'record_point.html',
				url: '/pages/record_point.html',
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		}, false);
*/
		doc.getElementById('kap_point').addEventListener('tap', function() {
			//打开积分记录页
			mui.openWindow({
				id: 'account_detail.html',
				url: '/pages/wallet/account_detail.html',
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		}, false);

		/*打开余额链托管账户页*/
		doc.getElementById('chain').addEventListener('tap', function() {
			//打开余额链托管页
			mui.openWindow({
				id: 'chain.html',
				url: '/pages/wallet/chainasset.html',
				show: {
					autoShow: true
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		}, false);

		/*
				doc.getElementById('rollin').addEventListener('tap', function() {
					//打开转入页
					mui.openWindow({
						id: 'roll_in.html',
						url: '/pages/order/roll_in.html',
						show: {
							aniShow: 'pop-in'
						},
						waiting: {
							autoShow: false
						}
					});
				}, false);

				doc.getElementById('roll_out').addEventListener('tap', function() {
					//打开转出页
					mui.openWindow({
						id: 'roll_out.html',
						url: '/pages/order/roll_out.html',
						show: {
							aniShow: 'pop-in'
						},
						waiting: {
							autoShow: false
						}
					});
				}, false);
		*/
		window.addEventListener('refresh_data', function(event) {
			userinfo = app.getState() || {};

			doc.getElementById('remaining').innerHTML = userinfo.a1_money;
			doc.getElementById('integral').innerHTML = userinfo.a2_money;
			doc.getElementById('kapay').innerHTML = userinfo.a1_money;
		});

	})

}(mui, document));